package com.zhangzhan.gulimallorder.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.zhangzhan.gulimallorder.vos.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {


    public String pay(PayVo vo) throws AlipayApiException {

        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());

        // 构造请求参数以调用接口
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();

        // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
        // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
        request.setNotifyUrl("http://sqgzjv.natappfree.cc/alipayed");
        // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
        //同步通知，支付成功，一般跳转到成功页
        request.setReturnUrl("http://member.gulimall.com/memberOrderList.html");

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();

        // 设置订单标题
        model.setSubject(subject);

        //订单超时时间 自动收单一分钟
        model.setTimeoutExpress("1m");


        // 设置商户订单号
        model.setOutTradeNo(out_trade_no);

        // 设置产品码
        model.setProductCode("FAST_INSTANT_TRADE_PAY");

        // 设置订单总金额
        model.setTotalAmount(total_amount);

        request.setBizModel(model);
        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

        AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "POST");
        // 如果需要返回GET请求，请使用
        // AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "GET");
        String pageRedirectionData = response.getBody();
        System.out.println(pageRedirectionData);

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }


        return pageRedirectionData;

    }

    public static AlipayConfig getAlipayConfig() {
        String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCyffG1W60U0m36Q652ik+ReInrqh/NrhTarK6JEUiXmokTTIJ7AEBYzV/IUHKSQvkNMchSAOF3wWSL5Xmt5czBnF4JjN5T6Ydcb7+IaRck57V+lgIwk/BNCVqwxf2gnY0l/6qSJkUkfJTT0r6SGypcE74Sxk7wepKv+JwNVRXe9nP3lvxnihRyUDo0GdNu4aFzWpyU45oXIv09JOP9XEhE8aoBiB95ev/VtfbqupDvI4AKH7fwpTHd43k82KUulsfw9o6OC9uQcGklCAjI5jusV/R5IAZMyIUCCoWzJa8DWlQURKNvUNgCP7BGtrr3YeQeUBN5imLycKlMv++r2BzxAgMBAAECggEBALFhehiaP7pmmQbRxgplz0co/B9oVOzJKE5ct6Lduz92Qu0b5run90qI5rwiPond5rVXGNVtrKOfXHWumirYKZDq/YUOKlBHzs5s+3HrxiVV48cl+o0eJ/vW5Pqx+ChrCj+OAYv2XlYxPJH5aIu4VXNl50mvK/nORgJ81VBmkzKcGsFL2YWqKaM17igyWttJ5+1t1VO3QJ7oKLohXMw/IItNkjb/m+TglTRz6tOPlvm+iGwj2D8Bc1HNudRIq7hfm/cV2G8MBMPy2vRNe8r6HAHJnIqDlaVAgn4VqMLhRywwWiT3XGHyGzzMljcy8zGQs34iLz/Y+EcBWYCBs7wpOxUCgYEA9M6471kDb/WOq09DD1QUOXXyb1IKf2pm2n8Kzj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YlmywNuvQbbjsXRjC+VsjzasUfZpLGHNjuS7FGwOr0H25Brtaogdgx1BZc495vQFgoAYMBPCzX7+6D1TCrZeGLglyTEsSIYOWlnbQDgjD32kPtfdTyLaSGXv8Ykjs8QtOHGdvsUqVNSfeF1IEp5wIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000138683680");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }
}
